#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

int deleteAndEarn(vector<int>& nums) {
    const int N = 10010;
    int arr[N] = { 0 };
    for (auto it : nums)
    {
        arr[it]++;
    }

    vector<int>f(N), g(N);
    f[0] = arr[0];
    for (int i = 1; i < N; i++)
    {
        f[i] = g[i - 1] + i * arr[i];
        g[i] = max(f[i - 1], g[i - 1]);
    }

    return max(f[N - 1], g[N - 1]);
}